//
//  BDMyWalletViewController.m
//  BadBoy
//
//  Created by wen on 2017/9/7.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDMyWalletViewController.h"
#import "BDUserModel.h"
#import "BDTransactionRecordsViewController.h"
#import "BDPayViewController.h"
#import "BDTixianViewController.h"
@interface BDMyWalletViewController ()

@end

@implementation BDMyWalletViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = ASLocalizedString(@"余额");
    
    UIButton *transactionButton = [UIButton buttonWithType:UIButtonTypeCustom];
    transactionButton.frame = CGRectMake(0, 0, 60, 40);
    [transactionButton setTitle:ASLocalizedString(@"交易记录")forState:UIControlStateNormal];
    [transactionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    transactionButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [[transactionButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        BDTransactionRecordsViewController *transactionRecordsVC = [[BDTransactionRecordsViewController alloc] init];
        [self.navigationController pushViewController:transactionRecordsVC animated:YES];
    }];
    
    
    UIBarButtonItem *recordButton = [[UIBarButtonItem alloc] initWithCustomView:transactionButton];
    self.navigationItem.rightBarButtonItem = recordButton;

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self refreshUsersData];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

-(void)refreshUsersData{
    NSString *url = [NSString stringWithFormat:@"%@users/%ld?need_withdraw_config=1",KBDBaseURL,[BDUserModel getCurrentUser].user_id];
    
    NSString *headValid = [BDUserModel getHeaderValidation];
    RACSignal *signal = [[BDNetworkingServer yy_GET:url parameters:nil validation:headValid automaticProcessingError:YES] execute:nil];
    
    [signal subscribeNext:^(id  _Nullable x) { 
        BDUserModel *user = [BDUserModel mj_objectWithKeyValues:x];
        [user saveCurrentUser];
        
          [self customView];
    }];
    
    [signal subscribeError:^(NSError * _Nullable error) {
        NSLog(@"%ld",error.code);
    }];
    
}

-(void)customView{
    UIView *backgroundView = [[UIView alloc] init];
    backgroundView.left = 0;
    backgroundView.top = kBDTopHeight;
    backgroundView.size = CGSizeMake(kBDScreenWidth, kBDScreenHeight - kBDTopHeight);
    backgroundView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backgroundView];
    
    BDUserModel *user = [BDUserModel getCurrentUser];
    
    UIImageView *avatorImageView = [[UIImageView alloc] init];
    avatorImageView.top = 20;
    avatorImageView.size = CGSizeMake(kBDScreenWidth - 100, 229);
    avatorImageView.centerX = backgroundView.width / 2;
    avatorImageView.image = [UIImage imageNamed:@"beijing2"];
    [backgroundView addSubview:avatorImageView];
    
    
    
    NSString *moneyString = [NSString stringWithFormat:@"¥ %@", @(user.balance)];
    
    UILabel *moneylabel = [[UILabel alloc] init];
    moneylabel.top = avatorImageView.bottom ;
    moneylabel.textColor = [UIColor colorWithHexString:@"ff992b"];
    moneylabel.font = [UIFont systemFontOfSize:50];
    
    NSMutableAttributedString *attribuString = [[NSMutableAttributedString alloc] initWithString:moneyString];
    NSRange range = [moneyString rangeOfString:@"¥"];
    [attribuString setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} range:range];
    moneylabel.attributedText = attribuString;
    
    [moneylabel sizeToFit];
    moneylabel.centerX = avatorImageView.centerX;
    [backgroundView addSubview:moneylabel];
    
    
    UILabel *balanceLabel = [[UILabel alloc] init];
    balanceLabel.top = moneylabel.bottom + 15;
    balanceLabel.text = [NSString stringWithFormat:ASLocalizedString(@"金额超过%ld元才可以提现喔!"),user.limit];
    balanceLabel.font = [UIFont systemFontOfSize:14];
    balanceLabel.textColor = [UIColor colorWithHexString:@"999999"];
    [balanceLabel sizeToFit];
    balanceLabel.centerX = avatorImageView.centerX;
    [backgroundView addSubview:balanceLabel];
    
    UIButton *rechargeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rechargeButton.top = balanceLabel.bottom + 10;
    [rechargeButton setTitle:ASLocalizedString(@"余额交易")forState:UIControlStateNormal];
    [rechargeButton setTitleColor:[UIColor colorWithHexString:@"ff992b"] forState:UIControlStateNormal];
    rechargeButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [rechargeButton sizeToFit];
    rechargeButton.centerX = avatorImageView.centerX;
    [rechargeButton addTarget:self action:@selector(rechargeActon) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:rechargeButton];
    
    
    UIButton *FAQButton = [UIButton buttonWithType:UIButtonTypeCustom];
    FAQButton.left = 0;
    FAQButton.size = CGSizeMake(kBDScreenWidth, 45);
    FAQButton.bottom = kBDScreenHeight - kBDTopHeight;
    [FAQButton setTitle:ASLocalizedString(@"提现")forState:UIControlStateNormal];
    [FAQButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    FAQButton.backgroundColor = [UIColor colorWithHexString:@"ff992b"];
    [FAQButton addTarget:self action:@selector(FAQAction) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:FAQButton];
}

-(void)rechargeActon{
    BDTransactionRecordsViewController *transactionRecordsVC = [[BDTransactionRecordsViewController alloc] init];
    [self.navigationController pushViewController:transactionRecordsVC animated:YES];
    
    /*
     BDPayViewController *payVc = [[BDPayViewController alloc] init];
     payVc.payType = BDProductPayTypeTopUp;
     [self.navigationController pushViewController:payVc animated:YES];
     */
}

-(void)FAQAction{
    BDTixianViewController *tixianVC = [[BDTixianViewController alloc] init];
    tixianVC.dict = nil;
    tixianVC.type = @"1";
    [self.navigationController pushViewController:tixianVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

